1389C - Good String - CodeForces Solution


brute force dp greedy two pointers *1500

Please click on ads to support us..

Python Code:

[2, 3, 4, 5, 6, 1]
[6, 1, 2, 3, 4, 5]


def len_merged(lhs, rhs):
	if len(lhs) == 0 or len(rhs) == 0:
		return 0
	if lhs[0] > rhs[0]:
		lhs, rhs = rhs, lhs
	i, j = 0, 0
	prev_c = 'r'
	s_len = 0
		while i < len(lhs) and j < len(rhs):
		if prev_c == 'r': 			if rhs[j] < lhs[i]:
				j += 1
			else:
				prev_c = 'l'
				s_len += 1
		elif prev_c == 'l':
			if lhs[i] < rhs[j]:
				i += 1
			else:
				prev_c = 'r'
				s_len += 1

		if (i == len(lhs) and lhs[-1] < rhs[-1]) or (j == len(rhs) and rhs[-1] < lhs[-1]):
		s_len += 1


	return s_len - s_len % 2


t = int(input())
for _ in range(t):
	s = input()
	idxs = list()
	max_len = -1
	for _ in range(10):
		idxs.append(list())
	for idx, c in enumerate(s):
		idxs[int(c)].append(idx)
	for idx_arr in idxs:
		max_len = max(max_len, len(idx_arr))

	for i in range(10):
		for j in range(i+1, 10):
			lhs, rhs = idxs[i], idxs[j]
			max_len = max(max_len, len_merged(lhs, rhs))
	print(len(s) - max_len)

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define all(v) v.begin(),v.end()
#define pb push_back
void solve()
{
    string s; cin >> s;
    if (s.size() <= 2)
    {
        cout << 0 << "\n";
    }
    else
    {
        vector<ll> v[10];
        for (ll i = 0; i < s.size(); ++i)
        {
            v[s[i] - '0'].pb(i);
        }
        ll i1 = -1, ans = LLONG_MAX;
        for (ll i = 0; i <= 9; ++i)
        {
            if (v[i].size() > 0)
            {
                for (ll j = 0; j <= 9; ++j)
                {
                    i1 = v[i][0];
                    ll temp = 0;
                    if (i == j) temp = 1;
                    if (v[j].size() == 0) continue;
                    while (1)
                    {
                        ll x = upper_bound(all(v[j]), i1) - v[j].begin();
                        if (x == v[j].size())
                        {
                            break;
                        }
                        else
                        {
                            if (i == j)
                                temp += 1;
                            else
                                temp += 2;
                            ll x1 = upper_bound(all(v[i]), v[j][x]) - v[i].begin();
                            if (x1 == v[i].size()) break;
                            if (i == j) temp++;
                            i1 = v[i][x1];
                        }
                    }
                    ans = min(ans, s.size() - temp);
                }
            }
        }
        cout << ans << "\n";
    }
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1528B - Kavi on Pairing Duty
339B - Xenia and Ringroad
189A - Cut Ribbon
1182A - Filling Shapes
82A - Double Cola
45A - Codecraft III
1242A - Tile Painting
1663E - Are You Safe
1663D - Is it rated - 3
1311A - Add Odd or Subtract Even
977F - Consecutive Subsequence
939A - Love Triangle
755A - PolandBall and Hypothesis
760B - Frodo and pillows
1006A - Adjacent Replacements
1195C - Basketball Exercise
1206A - Choose Two Numbers
1438B - Valerii Against Everyone
822A - I'm bored with life
9A - Die Roll
1430B - Barrels
279B - Books
1374B - Multiply by 2 divide by 6
1093B - Letters Rearranging
1213C - Book Reading
1468C - Berpizza
1546B - AquaMoon and Stolen String
1353C - Board Moves
902A - Visiting a Friend
299B - Ksusha the Squirrel